package com.yang.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.yang.mapper.ProviderMapper;
import com.yang.pojo.Provider;
import com.yang.pojo.User;
import com.yang.service.ProviderService;
import com.yang.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.support.SimpleTriggerContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/sys/provider")
public class ProviderController {
    @Autowired
    private ProviderService providerService;
    //查询所有供应商
    @ResponseBody
    @RequestMapping(value = "/queryAll",method = RequestMethod.GET,
            produces = {"application/json;charset=utf-8"})
    public String queryAll(){
        List<Provider> list = providerService.queryAll();
        return JSON.toJSONString(list);
    }

    /**
     * 分页显示供应商信息，并通过编号和姓名查找
     * @param provider
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping(value = "toList",method = RequestMethod.GET)
    public String toList(Provider provider,Integer pageNum, Integer pageSize, Model model){
        if (pageNum==null){
            pageNum = 1;
        }if (pageSize==null){
            pageSize = 5;
        }
        PageInfo<Provider> PageInfo = providerService.queryByCodeAndName(provider, pageNum, pageSize);
        model.addAttribute("pageInfo",PageInfo);
        model.addAttribute("provider",provider);
        return "provider_list";
    }

    /**
     * 根据id查找
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryById",method = RequestMethod.GET,
            produces = "application/json;charset=utf-8")
    public String queryById(Integer id){
        Provider provider = providerService.queryById(id);
        return JSON.toJSONString(provider);
    }
    @RequestMapping("/queryById2")
    public String queryById2(Model model,Integer id){
        Provider provider = providerService.queryById(id);
        model.addAttribute("provider",provider);
        return "provider_detail";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(
            value = "/delete",
            method = RequestMethod.GET)
    public String delete(Integer id){
        Integer count = providerService.delete(id);
        if (count>0){
            return "success";
        }
        return "error";
    }

    /**
     * 前往添加页面，并进行供应商添加
     * @return
     */
    @RequestMapping("/toSave")
    public String toSave(){
        return "provider_save";
    }

    @ResponseBody
    @RequestMapping(value = "/check",method = RequestMethod.GET)
    public String queryByCode(String code){
        List<Provider> list = providerService.queryByCode(code);
        if (null!=list && list.size()>0){
            //编号重复
            return "error";
        }
        //没重复
        return "success";
    }

    //添加供应商方法
    @ResponseBody
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(Provider provider, HttpSession session){
        //获取创建者
        User loginUser = (User)session.getAttribute(Common.LOGIN_USER);
        provider.setCreatedBy(loginUser.getId());
        Integer count = providerService.addProvider(provider);
        if (count>0){
            return "success";
        }
        return "error";
    }

    /**
     * 前往修改页面，并进行修改
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/toUpdate",method = RequestMethod.GET)
    public String toUpdate(Integer id,Model model){
        Provider provider = providerService.queryById(id);
        model.addAttribute("provider",provider);
        return "provider_update";
    }

    @ResponseBody
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(Provider provider,HttpSession session){
        User loginUser = (User)session.getAttribute(Common.LOGIN_USER);
        provider.setModifyBy(loginUser.getId());
        Integer count = providerService.update(provider);
        if (count>0){
            return "success";
        }
        return "error";
    }
}
